<?php

namespace model;

require_once 'User.php';

class LoginModel{
	
	/**
	 * \model\User class that contains valid user info
	 */
	public $user;
	
	/**
	 * String the salt used when crypting the pass
	 */
	private $passwordSalt = "ep";
	
	/**
	 * String Valid username
	 */
	private $realUserName = "Admin";

	/**
	 * String Valid password
	 */
	private $realPassword = "Password";
	
	/**
	 * int What login message to display
	 */
	private $messageId;
	
	/**
	 * String session loggedIn variable
	 */
	private $sessionLoggedInVariable = "loggedIn";
	
	/**
	 * String session User agent variable
	 */
	private $sessionUserAgentVariable = "webbläsare";
	
	/**
	 * String session loggedIn variable
	 */
	private $sessionIpVariable = "ip";
	
	public function __construct(){
		$this -> user = new User();
	}	
	
	/**
	 * @return boolean True if user is logged in
	 */
	public function isLoggedInBySession(){
		if($this -> checkSession()){
			if($_SESSION[$this -> sessionLoggedInVariable] == TRUE){
				$this -> messageId = 0;
				return TRUE;				
			} else{
				return FALSE;
			}
		} else{
			return FALSE;
		}
	}
	
	/**
	 * @return boolean True if the user has valid login cookies
	 * @param $cookiesExist
	 * @param $cookiesAreValid
	 */
	public function isLoggedInByCookies($cookiesExist, $cookiesAreValid){
		if($cookiesExist == TRUE && $cookiesAreValid == TRUE){
			$this -> messageId = 2;
			return TRUE;
		} elseif($cookiesExist == TRUE && $cookiesAreValid == FALSE){
			$this -> messageId = 8;
			return FALSE;
		} else {
			$this -> messageId = 0;
			return FALSE;
		}
	}
	
	/**
	 * @return String Hashed password
	 */
	public function getHashedPassword(){
		return crypt($this -> user -> password, $this -> passwordSalt);
	}
	
	/**
	 * @param $userName 
	 * @param $password
	 * @param $rememberMe whether or not the user has ticked the remember me checkbox
	 * @return boolean whether the user used a correct username/password combination in the login attempt
	 */
	public function checkUserInfo($userName, $password, $rememberMe){
		if($userName == $this -> user -> userName 
		&& $password == $this -> user -> password){
			$this -> setSession();
			$this -> messageId = 1;
			if($rememberMe){
				$this -> messageId = 3;
			}
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	/**
	 * @param $userName the username that was used in the login attempt
	 * @param $password the password that was used in the login attempt
	 * @return int loginMessageType type
	 */
	public function typeOfLogin($userName, $password){
		if($userName == ""){
			$this -> messageId = 5;
		} elseif($userName != "" && $password == ""){
			$this -> messageId = 6;
		} elseif($userName != $this -> user -> username){
			$this -> messageId = 7;
		}
	}
	
	/**
	 * @return int returns the id of the appropriate message to display after a login attempt
	 */
	public function getMessageId(){
		//return 1;
		return $this -> messageId;
	}
	
	/**
	 * Sets the session when the user logs in
	 */
	public function setSession(){
		$_SESSION[$this -> sessionUserAgentVariable] = $_SERVER["HTTP_USER_AGENT"];		
		$_SESSION[$this -> sessionIpVariable] = $_SERVER["REMOTE_ADDR"];
		$_SESSION[$this -> sessionLoggedInVariable] = TRUE;
	}
	
	/**
	 * sets the session to not logged in
	 */
	public function didUserLogOut(){
		if(isset($_SESSION[$this -> sessionLoggedInVariable])){
			if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST' && 
			$_SESSION[$this -> sessionLoggedInVariable] == TRUE) {
			$_SESSION[$this -> sessionLoggedInVariable] = FALSE;
			$this -> messageId = 4;
			return TRUE;			
			} else {
				return FALSE;
			}
		} else {
			return FALSE;
		}		
	}
	
	/**
	 * Returns true if the session truly belongs to the user
	 *  @return Boolean
	 */
	public function checkSession() {
		if(isset($_SESSION[$this -> sessionUserAgentVariable]) &&
			 isset($_SESSION[$this -> sessionIpVariable]) &&
			 isset($_SESSION[$this -> sessionLoggedInVariable])){
			if ($_SESSION[$this -> sessionUserAgentVariable] == $_SERVER["HTTP_USER_AGENT"] && 
			$_SESSION[$this -> sessionIpVariable] == $_SERVER["REMOTE_ADDR"] &&
			$_SESSION[$this -> sessionLoggedInVariable] == TRUE) {
				return TRUE;
			} else {
				return FALSE;
			}
		} else {
			return FALSE;
		}
	}
		
	public function setCookieTimer(){
		$endTime = time () + 60;
		file_put_contents("cookieExpiry.txt", $endTime);
	}
	
	/**
	 * @return time()
	 */
	public function getCookieTimer(){
		return file_get_contents("cookieExpiry.txt");
	}
}


